library(tidyverse)
library(plotly)
library(gganimate)
library(gifski)
data_cleaned <- read.csv("../data/cleaned.csv")
rate_year <- data_cleaned %>%
group_by(year) %>%
summarize(rate = length(which(college_attendance_status=="Yes"))/n())
pyear = ggplot(rate_year, aes(x=year, y=rate)) +
geom_line() +
geom_point() +
labs(title = "Trend Over Time", x="Year", y= "College Attendence Rate") +
scale_x_continuous(breaks = seq(2008, 2018, 2)) +
transition_reveal(year)
animate(pyear, duration = 5, fps = 20, width = 400, height = 400, renderer = gifski_renderer())

rate_city <- data_cleaned %>%
group_by(year, metropolitan_status) %>%
summarize(rate = length(which(college_attendance_status=="Yes"))/n())
# pivot_wider(names_from =metropolitan_status, values_from = rate)
pcity = ggplot(rate_city, aes(x=year, y=rate, color = metropolitan_status)) +
geom_line() +
geom_point() +
scale_x_continuous(breaks = seq(2008, 2018, 2)) +
labs(title = "Trend Over Time by Metropolitan", x="Year", y= "College Attendence Rate") +
transition_reveal(year)
animate(pcity, duration = 5, fps = 20, width = 600, height = 400, renderer = gifski_renderer())

# plot_ly(rate_city, x=~year, y=~`in metropolitan and central city`, name = "in metropolitan and central city", type = "scatter", mode = "line") %>%
# add_trace(y=~`in metropolitian but mixed of central city`, name = "in metropolitian but mixed of central city", type = "scatter", mode = "line") %>%
# add_trace(y=~`in metropolitian but not in central city`, name = "in metropolitian but not in central city", type = "scatter", mode = "line") %>%
# add_trace(y=~`mixed of metropolitian status`, name = "mixed of metropolitian status", type = "scatter", mode = "line") %>%
# add_trace(y=~`Not in metropolitan area`, name = "Not in metropolitan area", type = "scatter", mode = "line")# plot_ly(rate_city, x=~year, y=~`in metropolitan and central city`, name = "in metropolitan and central city", type = "scatter", mode = "line") %>%
# add_trace(y=~`in metropolitian but mixed of central city`, name = "in metropolitian but mixed of central city", type = "scatter", mode = "line") %>%
# add_trace(y=~`in metropolitian but not in central city`, name = "in metropolitian but not in central city", type = "scatter", mode = "line") %>%
# add_trace(y=~`mixed of metropolitian status`, name = "mixed of metropolitian status", type = "scatter", mode = "line") %>%
# add_trace(y=~`Not in metropolitan area`, name = "Not in metropolitan area", type = "scatter", mode = "line")
rate_state <- data_cleaned %>%
group_by(year, states) %>%
summarize(rate = length(which(college_attendance_status=="Yes"))/n()) %>%
mutate(rank = min_rank(-rate)) %>%
ungroup()
# pivot_wider(names_from =metropolitan_status, values_from = rate)
pstate <-ggplot(rate_state,aes(rank,group=states,fill=as.factor(states),color=as.factor(states))) +
geom_tile(aes(y = rate/2,height = rate, width = 0.9), alpha = 0.8, color = NA) +
geom_text(aes(y = 0, label = paste(states, ' ')), vjust = 0.2, hjust = 1) +
geom_text(aes(y=rate,label = paste(' ',rate)), hjust=0)+
coord_flip(clip = 'off', expand = TRUE) +
scale_y_continuous(labels = scales::comma) +
scale_x_reverse() +
guides(color = FALSE, fill = FALSE) +
theme_minimal() +
theme(
plot.title=element_text(size=25, hjust=0.5, face='bold', colour='grey', vjust=-1),
plot.subtitle=element_text(size=18, hjust=0.5, face='italic', color='grey'),
plot.caption =element_text(size=8, hjust=0.5, face='italic', color='grey'),
axis.ticks.y = element_blank(),
axis.text.y = element_blank(),
plot.margin = margin(1,1,1,4, 'cm')
)
pstate <- pstate + transition_states(states = year, transition_length = 4, state_length = 1) +
ease_aes('cubic-in-out') +
labs(title = 'College Attendance Rate per Year : {closest_state}',
x='',y='Total Suicides per year')
animate(pstate,fps = 20,duration = 30, width = 950, height = 750, renderer = gifski_renderer())
